package com.shigu.mo.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.opentae.data.mall.beans.MoBankLinkno;
import com.opentae.data.mall.examples.MoBankLinknoExample;
import com.opentae.data.mall.interfaces.MoBankLinknoMapper;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 查询联行号
 */
@Component
public class LinknoUtil {
    private static final Logger logger = LoggerFactory.getLogger(LinknoUtil.class);

    @Autowired
    MoBankLinknoMapper moBankLinknoMapper;

    /**
     * 如果联行号不存在则写入
     * @param bankName
     * @param bankCard
     * @param bankLocation
     * @return
     */
    public String writeLinkNo(String bankName,String bankCard,String bankLocation){
        MoBankLinknoExample example=new MoBankLinknoExample();
        example.createCriteria().andBankCardEqualTo(bankCard);
        List<MoBankLinkno> linknos=moBankLinknoMapper.selectByExample(example);
        if (linknos.size()>0) {
            return linknos.get(0).getLinkno();
        }
        MoBankLinkno linkno=new MoBankLinkno();
        linkno.setBankName(bankName);
        linkno.setBankCard(bankCard);
        linkno.setBankLocation(bankLocation);
        linkno.setLinkno(activeSelLinkNo(bankName,bankCard,bankLocation));
        moBankLinknoMapper.insertSelective(linkno);
        return linkno.getLinkno();
    }

    /**
     * 动态查询联行号
     * @return
     */
    public String activeSelLinkNo(String bankName,String bankCard,String bankLocation){
        String host = "http://lhh.market.alicloudapi.com";
        String path = "/lhh";
        String method = "GET";
        String appcode = "88aa4d33e59c40538ad238198ba57310";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("bankcard",bankCard);
        querys.put("bankname", bankName);
//        querys.put("city", "city");
//        querys.put("district", "district");
        querys.put("keyword", bankLocation);
        querys.put("page", "1");
//        querys.put("province", "province");
        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
            JSONObject obj=JSONObject.parseObject(EntityUtils.toString(response.getEntity()));
            JSONObject result=obj.getJSONObject("result");
            if(result.containsKey("list")){
                JSONArray list=result.getJSONArray("list");
                if (list.size()>0) {
                    JSONObject first=list.getJSONObject(0);
                    return first.getString("number");
                }
            }
            //获取response的body
            //System.out.println(EntityUtils.toString(response.getEntity()));
        } catch (Exception e) {
            logger.error("获取联行号失败",e);
        }
        return "102331000364";//随便放个
    }
}
